//
//  LoreDetailViewController.m
//  项目三
//
//  Created by rimi1 on 2016/12/14.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "LoreDetailViewController.h"

#import "CMDetailFoodView.h"
#import "NotView.h"

#import "NetRequest+Lore.h"

@interface LoreDetailViewController () <UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) CMDetailFoodView *footView;
@property (nonatomic, strong) NSDictionary *dataDic;
@property (nonatomic, strong) NotView *notView;

@end

@implementation LoreDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeAppearance];
}

#pragma mark - initialize

- (void)initializeDataSource {
    [self urlRequest];
}

- (void)initializeAppearance {
    [self.navigationBar removeFromSuperview];
    [self.view addSubview:self.webView];
    [self.view addSubview:self.footView];
}

#pragma mark - URLRequest

- (void)urlRequest {
    [NetRequest getLoreShowRequestWithParameter:@{@"id":self.loreId} succeed:^(id response) {
        if ([response[@"status"] integerValue] == 1) {
            [self.notView removeFromSuperview];
            NSMutableDictionary *dic = [response mutableCopy];
            [self.webView loadHTMLString:dic[@"message"] baseURL:nil];
        }else {
            [self.view insertSubview:self.notView belowSubview:self.footView];
        }
        
    } failure:^(NSError *error) {
        [self.view insertSubview:self.notView belowSubview:self.footView];
    }];
}

#pragma mark - respondsTo..

- (void)respondsToButton {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UIWebViewDelegate

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    CGSize size = webView.scrollView.contentSize;
    size.width = 0;
    webView.scrollView.contentSize = size;
}

#pragma mark - get

- (UIWebView *)webView {
    
    if (!_webView) {
        // y置顶
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kWIDTH, kHEIGHT - 49)];
        // 是否 不透明(不设这个值背景始终都是白色的，设置了的话，可以直接设置他的背景颜色)
        _webView.opaque = NO;
        _webView.backgroundColor = [UIColor whiteColor];
        _webView.scrollView.bounces = NO;
        _webView.delegate = self;
        
    }
    return _webView;
}

- (CMDetailFoodView *)footView {
    
    if (!_footView) {
        
        _footView = [[CMDetailFoodView alloc] initWithFrame:CGRectMake(0, kHEIGHT - 49, kWIDTH, 49)];
        _footView.layer.shadowOffset = CGSizeMake(0, -5);
        _footView.layer.shadowOpacity = 0.15;
        [_footView.collect removeFromSuperview];
        
        __weak LoreDetailViewController *detail = self;
        _footView.buttonBlock = ^(UIButton *sender) {
            [detail respondsToButton];
        };
        _footView.backgroundColor = [UIColor whiteColor];
        
    }
    return _footView;
}

- (NotView *)notView {
    
    if (!_notView) {
        
        _notView = [[NotView alloc] initWithFrame:CGRectMake(0, 0, kWIDTH, kHEIGHT - 49)];
        _notView.backgroundColor = [UIColor whiteColor];
        _notView.title.text = @"加载出错....";
        
        __weak LoreDetailViewController *detail = self;
        _notView.tapRefresh = ^() {
            [detail urlRequest];
        };
        
    }
    return _notView;
}



@end









